﻿using Microsoft.AspNetCore.Http;

namespace Application.Utils
{
    public static class HttpContextExtension
    {
        public static string RealIP(this HttpContext httpContent)
        {
            //若有反向代理，如nginx需设置：proxy_set_header X-Real-IP $remote_addr;
            var realIp = httpContent.Request.Headers["X-Real-IP"].ToString();
            //若有反向代理，如nginx需设置：proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            if (string.IsNullOrEmpty(realIp)) realIp = httpContent.Request.Headers["X-Forwarded-For"].ToString();
            if (string.IsNullOrEmpty(realIp)) realIp = httpContent.Connection.RemoteIpAddress.MapToIPv4().ToString();
            return realIp;
        }
    }
}
